import { App as VueApp, createApp as createVueApp, DefineComponent } from 'vue'
import { Router } from 'vue-router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import dictApi from '@/request/dict'
import { setDict } from '@/core/dict'

export interface AppOp {
  component: DefineComponent<{}, {}, any>
  router?: Router
  dom: string | HTMLElement
  ui: string
}
class App {
  rootVm: VueApp = null
  constructor(op: AppOp) {
    this.init(op)
  }
  private async init(op: AppOp) {
    // console.log(dictApi)
    let dict = await dictApi.loadDict('')
    setDict(dict)
    this.rootVm = createVueApp(op.component)
    if (op.router) {
      this.rootVm.use(op.router)
    }
    this.rootVm.use(ElementPlus, {
      locale: zhCn,
    })

    this.rootVm.mount(op.dom)
  }
}

export function createApp(op: AppOp) {
  return new App(op)
}
